package com.blacktry.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.service.DeriveExcelService;
import com.blacktry.entity.easypoidata.DeriveExcel;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-06-23
 */
@Api(value = "Excel管理模块", tags = "Excel管理接口")
@RestController
@RequestMapping("/Excel")
public class DeriveExcelController {

    @Autowired
    private DeriveExcelService deriveExcelService;

    @OperLog(operModul = "Excel管理-添加导出Excel数据",operType = OprLogConst.ADD,operDesc = "添加导出Excel数据")
    @ApiOperation("添加导出Excel数据")
    @PostMapping("/insert")
    public ApiResult<Integer> insert(@RequestBody DeriveExcel deriveExcel){
        return ApiResultHandler.buildApiResult(200,"插入成功",deriveExcelService.insert(deriveExcel));
    }

    @OperLog(operModul = "Excel管理-根据考试id查询导出所需数据",operType = OprLogConst.SELECT,operDesc = "根据考试id查询导出所需数据")
    @ApiOperation("根据考试id查询导出所需数据")
    @GetMapping("/select/{id}")
    public ApiResult<List<DeriveExcel>> select(@PathVariable Integer id){
        return ApiResultHandler.buildApiResult(200,"插入成功",deriveExcelService.select(id));
    }

    @OperLog(operModul = "Excel管理-查询所有导入导出数据",operType = OprLogConst.SELECT,operDesc = "查询所有导入导出数据")
    @ApiOperation("查询所有导入导出数据")
    @GetMapping("/select")
    public ApiResult<List<DeriveExcel>> selectExam(){
        return ApiResultHandler.buildApiResult(200,"成功",deriveExcelService.select());
    }

    @OperLog(operModul = "Excel管理-分页查看所有导入导出数据",operType = OprLogConst.SELECT,operDesc = "分页查看所有导入导出数据")
    @ApiOperation("分页查看所有导入导出数据")
    @PostMapping("/select/{page}/{size}")
    public ApiResult<Page<DeriveExcel>> findAll(@RequestBody DeriveExcel deriveExcel, @DefaultValue("1") @PathVariable("page") Integer page, @DefaultValue("6") @PathVariable("size") Integer size){
        System.out.println("分页查询所有导入导出数据");
        Page<DeriveExcel> deriveExcelPage = new Page<>(page,size);
        return ApiResultHandler.buildApiResult(200, "请求成功！",deriveExcelService.selectPage(deriveExcel,deriveExcelPage));
    }

    @OperLog(operModul = "Excel管理-添加/更新导入导出数据",operType = OprLogConst.ADD,operDesc = "添加/更新导入导出数据")
    @ApiOperation("添加/更新导入导出数据")
    @PostMapping("/updateOrInsert")
    public ApiResult<Integer> updateOrInsertExam(@RequestBody DeriveExcel deriveExcel){
        return ApiResultHandler.buildApiResult(200,"成功",deriveExcelService.updateOrInsert(deriveExcel));
    }

    @OperLog(operModul = "Excel管理-添加/更新导入导出数据2",operType = OprLogConst.ADD,operDesc = "添加/更新导入导出数据2")
    @ApiOperation("添加/更新导入导出数据")
    @PostMapping("/updateOrInsert2")
    public ApiResult<Integer> updateOrInsertExam2(@RequestBody DeriveExcel deriveExcel){
        return ApiResultHandler.buildApiResult(200,"成功",deriveExcelService.updateOrInsert2(deriveExcel));
    }

    @OperLog(operModul = "Excel管理-批量删除导出数据",operType = OprLogConst.DELETE,operDesc = "批量删除导出数据")
    @ApiOperation("批量删除导出数据")
    @DeleteMapping("/deleteBatchIds")
    public ApiResult<Integer> deleteBatchIds(String[] ids){
        return ApiResultHandler.buildApiResult(200,"成功",deriveExcelService.deleteBatchIds(ids));
    }

    @OperLog(operModul = "Excel管理-删除单个导出数据",operType = OprLogConst.DELETE,operDesc = "删除单个导出数据")
    @ApiOperation("删除单个导出数据")
    @DeleteMapping("/deleteOne/{id}")
    public ApiResult<Integer> deleteOne(@PathVariable("id") Integer id){
        return ApiResultHandler.buildApiResult(200,"成功",deriveExcelService.removeById(id));
    }

    @OperLog(operModul = "Excel管理-根据username和examId查询具体考试提交",operType = OprLogConst.SELECT,operDesc = "根据username和examId查询具体考试提交")
    @ApiOperation("根据username和examId查询具体考试提交")
    @GetMapping("/selectDeriveByExamIdAndUsername/{examId}/{username}")
    public ApiResult<List<DeriveExcel>> selectDeriveByExamIdAndUsername(@PathVariable("examId")String examId,@PathVariable("username")String username){
        return ApiResultHandler.buildApiResult(200,"成功",deriveExcelService.selectDeriveByExamIdAndUsername(examId,username));
    }

}

